package com.sinosoft.perfect.system._roleBusiness.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

import com.sinosoft.perfect.common.system.BaseAction;
import com.sinosoft.perfect.system._.po.TRoleBusiness;
import com.sinosoft.perfect.system._roleBusiness.dataFormat.IRoleBusinessGridDataFormat;
import com.sinosoft.perfect.system._roleBusiness.service.IRoleBusinessService;

@SuppressWarnings("serial")
public class RoleBusinessAction extends BaseAction {

	private IRoleBusinessService roleBusinessService;

	private IRoleBusinessGridDataFormat roleBusinessGridDataFormat;

	private TRoleBusiness troleBusiness;

	private String roleBusinessId;

	private String unitId;

	private String userId;

	/**
	 * 
	 */

	public String roleBusinessGrid() throws IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		String resultXml = null;
		try {
			List<TRoleBusiness> roleBusinesss = roleBusinessService.selectByTRoleBusiness(troleBusiness);
			resultXml = roleBusinessGridDataFormat.dataFormatRoleBusinessGrid(roleBusinesss);

		} catch (SQLException e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		PrintWriter out = response.getWriter();
		out.print(resultXml);
		out.flush();
		out.close();
		return null;
	}

	public String roleBusinessGridForBindUser() throws IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		String resultXml = null;
		try {
			List<TRoleBusiness> roleBusinesss = roleBusinessService.selectByTRoleBusiness(troleBusiness);
			List<String> roleBusinessIds = roleBusinessService.selectTRoleBusinessFromUser(userId);
			resultXml = roleBusinessGridDataFormat.dataFormatRoleBusinessGridForBindUser(roleBusinesss, roleBusinessIds);

		} catch (SQLException e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		PrintWriter out = response.getWriter();
		out.print(resultXml);
		out.flush();
		out.close();
		return null;
	}
	
	/**
	 * 为组织机构用户配置(fys 11-07-08)，主要是拷贝上面的方法，将grid的列变成一列
	 */
	public String roleBusinessGridForBindAUnitUsers() throws IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		String resultXml = null;
		try {
			List<TRoleBusiness> roleBusinesss = roleBusinessService.selectByTRoleBusiness(troleBusiness);
			List<String> roleBusinessIds = roleBusinessService.selectTRoleBusinessFromUser(userId);
			resultXml = roleBusinessGridDataFormat.roleBusinessGridForBindAUnitUsers(roleBusinesss, roleBusinessIds);

		} catch (SQLException e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		PrintWriter out = response.getWriter();
		out.print(resultXml);
		out.flush();
		out.close();
		return null;
	}

	public String roleBusinessGridForBindUnit() throws IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		String resultXml = null;
		try {
			List<TRoleBusiness> roleBusinesss = roleBusinessService.selectByTRoleBusiness(troleBusiness);
			List<String> roleBusinessIds = roleBusinessService.selectTRoleBusinessFromUnit(unitId);
			resultXml = roleBusinessGridDataFormat.dataFormatRoleBusinessGridForBindUser(roleBusinesss, roleBusinessIds);

		} catch (SQLException e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		PrintWriter out = response.getWriter();
		out.print(resultXml);
		out.flush();
		out.close();
		return null;
	}

	public String roleBusinessGridForUser() throws IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		String resultXml = null;
		try {
			List<TRoleBusiness> roleBusinesss = roleBusinessService.selectByTRoleBusiness(troleBusiness);
			resultXml = roleBusinessGridDataFormat.dataFormatRoleBusinessGridForUser(roleBusinesss);

		} catch (SQLException e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		PrintWriter out = response.getWriter();
		out.print(resultXml);
		out.flush();
		out.close();
		return null;
	}
	
	/**
	 * 为组织机构用户配置(fys 11-07-11)，主要是拷贝roleBusinessGridForBindUnit方法，将grid的列变成一列
	 */
	public String roleBusinessGridForBindAUnit() throws IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		String resultXml = null;
		try {
			List<TRoleBusiness> roleBusinesss = roleBusinessService.selectByTRoleBusiness(troleBusiness);
			List<String> roleBusinessIds = roleBusinessService.selectTRoleBusinessFromUnit(unitId);
			resultXml = roleBusinessGridDataFormat.roleBusinessGridForBindAUnit(roleBusinesss, roleBusinessIds);

		} catch (SQLException e) {
			e.printStackTrace();
			session.put("exceptionInfoClass", e);
			return "exceptionOperateAction";
		}
		PrintWriter out = response.getWriter();
		out.print(resultXml);
		out.flush();
		out.close();
		return null;
	}

	/**
	 * get/set
	 */

	public void setRoleBusinessService(IRoleBusinessService roleBusinessService) {
		this.roleBusinessService = roleBusinessService;
	}

	public void setRoleBusinessGridDataFormat(IRoleBusinessGridDataFormat roleBusinessGridDataFormat) {
		this.roleBusinessGridDataFormat = roleBusinessGridDataFormat;
	}

	public TRoleBusiness getTroleBusiness() {
		return troleBusiness;
	}

	public void setTroleBusiness(TRoleBusiness troleBusiness) {
		this.troleBusiness = troleBusiness;
	}

	public String getRoleBusinessId() {
		return roleBusinessId;
	}

	public void setRoleBusinessId(String roleBusinessId) {
		this.roleBusinessId = roleBusinessId;
	}

	public String getUnitId() {
		return unitId;
	}

	public void setUnitId(String unitId) {
		this.unitId = unitId;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}
}
